package com.handinfo.android.ui;

import android.graphics.Bitmap;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.game.IIcon;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.utils.Tools;

public class Soul implements IIcon {
	public static final byte 状态_未激活 = 0;
	public static final byte 状态_已激活 = 1;
	public static final byte 状态_正在激活 = 2;
	public static final byte 状态_正在升级 = 3;

	public String m_name;// 魂名
	public int m_index;//魂位
	public int m_page;//魂页
	public float m_x;
	public float m_y;
	public byte m_state;// 状态 0未激活 1正在激活 2已经激活
	public short m_num;// 计数
	public short m_max;// 最大计数
	public String m_value;
	public String m_priceByMoney;// 单价：钱
	public String m_priceBySpirit;// 单价：精魂
	public Bitmap m_未激活;// 图片
	public Bitmap m_已激活;
	public Actor[] m_正在激活;
	public int m_ActorIndex;//0正常 1升级
	
	
	public Soul(){
		m_未激活 = IconManger.m_soul[0];
		m_已激活 = IconManger.m_soul[1];
		m_正在激活 = IconManger.m_soulActor;
	}
	
	public void reset(){
		m_num = 0;
		m_max = 0;
		m_state = 0;
		m_value = null;
		m_priceByMoney = null;
		m_priceBySpirit = null;
	}
	
	public void logic(){
		if(m_state == 状态_正在激活 || m_state == 状态_正在升级 ){
			switch(m_ActorIndex){
			case 0://正常火
				if (m_正在激活[m_ActorIndex] != null && m_正在激活[m_ActorIndex].m_enabled) {
					m_正在激活[m_ActorIndex].nextFrame();
				} 
				break;
			case 1://升级
				if (m_正在激活[m_ActorIndex] != null && m_正在激活[m_ActorIndex].m_enabled) {
					m_正在激活[m_ActorIndex].nextFrame();
				}else{
					m_正在激活[m_ActorIndex].activate();
					m_正在激活[m_ActorIndex].setAction(0, false, true);
					m_ActorIndex = 0;
					if(isAlive() && m_num == m_max){						
						m_state = Soul.状态_已激活;
					}
				} 
				break;
			}
		}
	}
	
	/***
	 * 设置位置
	 * @param x
	 * @param y
	 */
    public void setPostion(float x,float y){
    	m_x = x;
    	m_y = y;
    }
    
	/**
	 * 判断当前魂殿是否已请求过数据
	 * */
	public boolean isAlive() {
		return m_priceByMoney != null ? true : false;
	}

	public void drawIcon(DWGraphics g, int x, int y, int w, int h, int anchor) {
		switch(m_state){
		case 状态_未激活:
			if(m_未激活 != null){
				g.drawBitmap(m_未激活, x, y, anchor);
			}
			break;
		case 状态_已激活:
			if(m_已激活 != null){
				g.drawBitmap(m_已激活, x, y, anchor);
			}
			break;
		case 状态_正在激活:
		case 状态_正在升级:
			if(m_未激活 != null){
				g.drawBitmap(m_未激活, x, y, anchor);
			}
			if(m_正在激活 != null && m_正在激活[m_ActorIndex] != null &&  m_正在激活[m_ActorIndex].m_enabled){
				m_正在激活[m_ActorIndex].m_posX = x;
				m_正在激活[m_ActorIndex].m_posY = y;
				m_正在激活[m_ActorIndex].draw(g, 0, 0);
			}
			break;
		}
		logic();
	}

	public void drawIcon(DWGraphics g, int x, int y, int w, int h,
			float scaleX, float scaleY, int anchor) {
		int icon_w = 0;
		int icon_h = 0;
		switch(m_state){
		case 状态_未激活:
			if(m_未激活 != null){
				icon_w = (int) (m_未激活.getWidth() * scaleX);
				icon_h = (int) (m_未激活.getHeight() * scaleY);
				g.drawBitmap(m_未激活, x, y, icon_w, icon_h, anchor);
			}
			break;
		case 状态_已激活:
			if(m_已激活 != null){
				icon_w = (int) (m_已激活.getWidth() * scaleX);
				icon_h = (int) (m_已激活.getHeight() * scaleY);
				g.drawBitmap(m_已激活, x, y, icon_w, icon_h, anchor);
			}
			break;
		case 状态_正在激活:
		case 状态_正在升级:
			if(m_未激活 != null){
				g.drawBitmap(m_未激活, x, y, anchor);
			}
			if(m_正在激活 != null && m_正在激活[m_ActorIndex] != null &&  m_正在激活[m_ActorIndex].m_enabled){
				m_正在激活[m_ActorIndex].m_posX = x;
				m_正在激活[m_ActorIndex].m_posY = y;
				m_正在激活[m_ActorIndex].draw(g, 0, 0);
			}
			break;
		}
		logic();
	}
 
 
}
